********************************************************************************
********************************************************************************
* Title: 		mean_comparison
* Project:		Stress, Ethnicity, and Prosocial Behavior
* Author:		Moritz Poll (moritz.poll@brown.edu)
* PIs:			Johannes Haushofer, Sara Lowes, Abednego Musau, David Ndetei, 
*				Nathan Nunn, Moritz Poll, Nancy Qian
* Purpose:		Plots the means from different games alongside relevant
*				significance levels
********************************************************************************
********************************************************************************

noisily dis as input "Start: Mean comparisons"

use  "$cleandata_dir/HIO_for_analysis_xlong.dta", clear

********************************************************************************
****************************** Retrieve significance ***************************
********************************************************************************
{
estimates use "$estimates_dir/cyd_spec1_nocontrols"
lincom _b[_same_eth]
local Pcyd "n.s."
if r(p) < 0.1  local Pcyd "*"
if r(p) < 0.05 local Pcyd "**"
if r(p) < 0.01 local Pcyd "***"
lincom _b[_same_eth] + _b[1.treatment#c._same_eth]
local Hcyd "n.s."
if r(p) < 0.1  local Hcyd "*"
if r(p) < 0.05 local Hcyd "**"
if r(p) < 0.01 local Hcyd "***"
lincom _b[1.treatment#c._same_eth]
local Dcyd "n.s."
if r(p) < 0.1  local Dcyd "*"
if r(p) < 0.05 local Dcyd "**"
if r(p) < 0.01 local Dcyd "***"

foreach game in dg tg1 tg2 social_proximity likely_to_be_friends trust closeness {
	estimates use "$estimates_dir/`game'_spec1_nocontrols"
	lincom _b[1._same_eth]
	local P`game' "n.s."
	if r(p) < 0.1  local P`game' "*"
	if r(p) < 0.05 local P`game' "**"
	if r(p) < 0.01 local P`game' "***"
	lincom _b[1._same_eth] + _b[1.treatment#1._same_eth]
	local H`game' "n.s."
	if r(p) < 0.1  local H`game' "*"
	if r(p) < 0.05 local H`game' "**"
	if r(p) < 0.01 local H`game' "***"
	lincom _b[1.treatment#1._same_eth]
	local D`game' "n.s."
	if r(p) < 0.1  local D`game' "*"
	if r(p) < 0.05 local D`game' "**"
	if r(p) < 0.01 local D`game' "***"
	lincom _b[1.treatment]
	local diff`game' "n.s."
	if r(p) < 0.1  local diff`game' "*"
	if r(p) < 0.05 local diff`game' "**"
	if r(p) < 0.01 local diff`game' "***"
	lincom _b[1.treatment] + _b[1.treatment#1._same_eth]
	local same`game' "n.s."
	if r(p) < 0.1  local same`game' "*"
	if r(p) < 0.05 local same`game' "**"
	if r(p) < 0.01 local same`game' "***"
}

********************************************************************************
******************************** Retrieve means ********************************
********************************************************************************

drop if game_str == "cyd" & !cyd_owneth_available
collapse decision share (semean) decision_se = decision share_se = share, by(game_str treatment _same_eth)
gen share_h = share + share_se
gen share_l = share - share_se
gen decision_h = decision + decision_se
gen decision_l = decision - decision_se

********************************************************************************
********************************* Basic Setup **********************************
********************************************************************************

local barwidth = 0.35
local offset = 0.2
gen offset = -`offset'
replace offset = -offset if _same_eth
replace offset = offset + 1 if treatment
gen aux = .
gen aux_left = 0
gen aux_right = 1
gen aux_high = .
gen aux_low = .
gen aux_high_tip = .
gen aux_low_tip = .
gen aux_pill_high = .
gen aux_pill_low = .
gen aux_pill_high_tip = .
gen aux_pill_low_tip = .
gen aux_leftleft = -`offset'
gen aux_leftright = `offset'
gen aux_rightleft = 1-`offset'
gen aux_rightright = 1+`offset'

local subtitlecyd "$cyd_name"
local subtitledg  "$dg_name"
local subtitletg1 "$tg1_name"
local subtitletg2 "$tg2_name"
local subtitlesocial_proximity "$social_proximity_name"
local subtitlelikely_to_be_friends "Likely to be friends"
local subtitletrust "Trust"
local subtitlecloseness "Closeness"
global barcolor gray
global rcapcolor black

capture discard
}
********************************************************************************
********************************** CYD Panel ***********************************
********************************************************************************

sum decision_h if game_str == "cyd"
local maxmax			= 0.78  			// stars across pills
local max				= 0.74  			// stars within pill
replace aux_low			= `max' - 0.015 	// bracket within pills (bar)
replace aux_low_tip		= `max' - 0.02		// bracket within pills (tips)
replace aux_high		= `maxmax' - 0.015 	// bracket across pills (bar)
replace aux_high_tip	= `maxmax' - 0.02	// bracket across pills (tips)

twoway 	(bar decision offset if game_str == "cyd" & !treatment & !_same_eth, barwidth(`barwidth') fcolor(none) lcolor($barcolor) lwidth(medium)) ///
		(bar decision offset if game_str == "cyd" & !treatment &  _same_eth, barwidth(`barwidth')  color($barcolor)) ///
		(bar decision offset if game_str == "cyd" &  treatment & !_same_eth, barwidth(`barwidth') fcolor(none) lcolor($barcolor) lwidth(medium)) ///
		(bar decision offset if game_str == "cyd" &  treatment &  _same_eth, barwidth(`barwidth')  color($barcolor)) ///
		(rcap decision_h decision_l offset if game_str == "cyd" & !treatment & !_same_eth, color($rcapcolor)) ///
		(rcap decision_h decision_l offset if game_str == "cyd" & !treatment &  _same_eth, color($rcapcolor)) ///
		(rcap decision_h decision_l offset if game_str == "cyd" &  treatment & !_same_eth, color($rcapcolor)) ///
		(rcap decision_h decision_l offset if game_str == "cyd" &  treatment &  _same_eth, color($rcapcolor)) ///
		(rcap aux_left aux_right aux_high, horizontal color(black) msize(0) text(`maxmax' 0.5 "`Dcyd'")) /// 
		(rcap aux_high aux_high_tip aux_left, color(black) msize(0)) ///
		(rcap aux_high aux_high_tip aux_right, color(black) msize(0)) ///
		(rcap aux_leftleft aux_leftright aux_low, horizontal color(black) msize(0) text(`max' 0 "`Pcyd'")) ///
		(rcap aux_low aux_low_tip aux_leftleft, color(black) msize(0)) ///
		(rcap aux_low aux_low_tip aux_leftright, color(black) msize(0)) ///
		(rcap aux_rightleft aux_rightright aux_low, horizontal color(black) msize(0) text(`max' 1 "`Hcyd'")) ///
		(rcap aux_low aux_low_tip aux_rightleft, color(black) msize(0)) ///
		(rcap aux_low aux_low_tip aux_rightright, color(black) msize(0)) ///
		, name(cyd, replace) scale(0.8) ///
		$bar_design ytitle("Mean likelihood of choosing a person", size(large)) xtitle("") ///
		subtitle("`subtitlecyd'", bcolor(white) size(large)) xscale(r(-0.5 1.5)) ///
		xlabel(0 "Placebo" 1 "Hydrocortisone", labsize(4.5) noticks) ylabel(0(0.1)0.8, nogrid) legend(off)
graph combine cyd, fxsize(40) nodraw name(cyd, replace) graphregion(margin(0 0 0 0) color(white)) imargin(0 1 0 0)

********************************************************************************
****************************** DG, TG1, TG2 Panel ******************************
********************************************************************************

sum share_h if game_str != "cyd" & game_str != "social_proximity"
local maxmax 				= 0.78 					// stars across pills
local max					= 0.74 					// stars within pill
local pillhigh 				= 0.7					// stars within different
local pilllow 				= 0.66 					// stars within same
replace aux_low				= `max' - 0.015 		// bracket within pills (bar)
replace aux_low_tip			= `max' - 0.02			// bracket within pills (tips)
replace aux_high			= `maxmax' - 0.015 		// bracket across pills (bar)
replace aux_high_tip		= `maxmax' - 0.02		// bracket across pills (tips)
replace aux_pill_high		= `pillhigh' - 0.015	// bracket within hydro pill (bar)
replace aux_pill_high_tip	= `pillhigh' - 0.02		// bracket within hydro pill (tips)
replace aux_pill_low		= `pilllow' - 0.015		// bracket within placebo pill (bar)
replace aux_pill_low_tip	= `pilllow' - 0.02		// bracket within placebo pill (tips)

local yscale    yscale(r(0(0.1)0.8))
local yscaledg  yscale(r(0(0.1)0.8))
local yscaletg1 yscale(r(0(0.1)0.8) off)
local yscaletg2 yscale(r(0(0.1)0.8) off)
local fxsizedg 39
local fxsizetg1
local fxsizetg2
local legenddg  legend(size(medium) order(1 "Different Ethnicity" 2 "Same Ethnicity" 5 "{&plusminus} 1 SE" 88 " " 88 " " 88 " " 88 " " 88 " " 88 " " 88 " " 88 " ") cols(1) region(color(none)) bplacement(c) ring(0)) // The 88 shift the legend up
local legendtg1 legend(off)
local legendtg2 legend(off)


foreach game in dg tg1 tg2 {
foreach scale_insert in "" "`game'" { // Run the middle parts of the graph twice, once giving tg1 and tg2 a y-axis and saving them and once leaving it out and preparing it for graph combine
twoway  (bar share offset if game_str == "`game'" & !treatment & !_same_eth, barwidth(`barwidth') fcolor(none) lcolor($barcolor) lwidth(medium)) ///
		(bar share offset if game_str == "`game'" & !treatment &  _same_eth, barwidth(`barwidth')  color($barcolor)) ///
		(bar share offset if game_str == "`game'" &  treatment & !_same_eth, barwidth(`barwidth') fcolor(none) lcolor($barcolor) lwidth(medium)) ///
		(bar share offset if game_str == "`game'" &  treatment &  _same_eth, barwidth(`barwidth')  color($barcolor)) ///
		(rcap share_h share_l offset if game_str == "`game'" & !treatment & !_same_eth, color($rcapcolor)) ///
		(rcap share_h share_l offset if game_str == "`game'" & !treatment &  _same_eth, color($rcapcolor)) ///
		(rcap share_h share_l offset if game_str == "`game'" &  treatment & !_same_eth, color($rcapcolor)) ///
		(rcap share_h share_l offset if game_str == "`game'" &  treatment &  _same_eth, color($rcapcolor)) ///
		(rcap aux_left aux_right aux_high, horizontal color(black) msize(0) text(`maxmax' 0.5 "`D`game''")) ///
		(rcap aux_high aux_high_tip aux_left, color(black) msize(0)) ///
		(rcap aux_high aux_high_tip aux_right, color(black) msize(0)) ///
		(rcap aux_leftleft aux_leftright aux_low, horizontal color(black) msize(0) text(`max' 0 "`P`game''")) ///
		(rcap aux_low aux_low_tip aux_leftleft, color(black) msize(0)) ///
		(rcap aux_low aux_low_tip aux_leftright, color(black) msize(0)) ///
		(rcap aux_rightleft aux_rightright aux_low, horizontal color(black) msize(0) text(`max' 1 "`H`game''")) ///
		(rcap aux_low aux_low_tip aux_rightleft, color(black) msize(0)) ///
		(rcap aux_low aux_low_tip aux_rightright, color(black) msize(0)) ///
		(rcap aux_leftleft aux_rightleft aux_pill_low, horizontal color(black) msize(0) text(`pilllow' `=0.5-`offset'' "`diff`game''")) ///
		(rcap aux_pill_low aux_pill_low_tip aux_leftleft, color(black) msize(0)) ///
		(rcap aux_pill_low aux_pill_low_tip aux_rightleft, color(black) msize(0)) ///
 		(rcap aux_leftright aux_rightright aux_pill_high, horizontal color(black) msize(0) text(`pillhigh' `=0.5+`offset'' "`same`game''")) ///
		(rcap aux_pill_high aux_pill_high_tip aux_leftright, color(black) msize(0)) ///
		(rcap aux_pill_high aux_pill_high_tip aux_rightright, color(black) msize(0)) ///
 		, name(`game', replace) scale(1.1) ///
		$bar_design ytitle("Mean share allocated", size(large)) xtitle("") `yscale`scale_insert'' ///
		subtitle("`subtitle`game''", bcolor(white) size(large)) xscale(r(-0.5 1.5)) ///
		xlabel(0 "Placebo" 1 "Hydrocortisone", labsize(medlarge) noticks) ylabel(0(0.1)0.8, nogrid) `legend`game''
graph combine `game', fxsize(`fxsize`game'') nodraw name(`game', replace) graphregion(margin(0 0 0 0) color(white)) imargin(0 1 0 0)
}
}
graph combine dg tg1 tg2, nodraw name(allocations, replace) ycommon imargin(0 1 0 0) rows(1) graphregion(color(white))


********************************************************************************
**************************** Social Proximity Panel ****************************
********************************************************************************

sum decision_h if game_str == "social_proximity"
local maxmax 				= 0.78					// stars across pills
local max 					= 0.74					// stars within pills
local pillhigh 				= 0.7					// stars within hydro pill
local pilllow 				= 0.66					// stars within placebo pill
replace aux_low				= `max' - 0.015			// bracket within pills (bar)
replace aux_low_tip			= `max' - 0.02			// bracket within pills (tips)
replace aux_high			= `maxmax' - 0.015		// bracket across pills (bar)
replace aux_high_tip		= `maxmax' - 0.02		// bracket across pills (tips)
replace aux_pill_high		= `pillhigh' - 0.015	// bracket within hydro pill (bar)
replace aux_pill_high_tip	= `pillhigh' - 0.02		// bracket within hydro pill (tips)
replace aux_pill_low		= `pilllow' - 0.015		// bracket within placebo pill (bar)
replace aux_pill_low_tip	= `pilllow' - 0.02		// bracket within placebo pill (tips)
                
twoway	(bar decision offset if game == "social_proximity" & !treatment & !_same_eth, barwidth(`barwidth') fcolor(none) lcolor($barcolor) lwidth(medium)) ///
		(bar decision offset if game == "social_proximity" & !treatment &  _same_eth, barwidth(`barwidth')  color($barcolor)) ///
		(bar decision offset if game == "social_proximity" &  treatment & !_same_eth, barwidth(`barwidth') fcolor(none) lcolor($barcolor) lwidth(medium)) ///
		(bar decision offset if game == "social_proximity" &  treatment &  _same_eth, barwidth(`barwidth')  color($barcolor)) ///
		(rcap decision_h decision_l offset if game == "social_proximity" & !treatment & !_same_eth, color($rcapcolor)) ///
		(rcap decision_h decision_l offset if game == "social_proximity" & !treatment &  _same_eth, color($rcapcolor)) ///
		(rcap decision_h decision_l offset if game == "social_proximity" &  treatment & !_same_eth, color($rcapcolor)) ///
		(rcap decision_h decision_l offset if game == "social_proximity" &  treatment &  _same_eth, color($rcapcolor)) ///
		(rcap aux_left aux_right aux_high, horizontal color(black) msize(0) text(`maxmax' 0.5 "`Dsocial_proximity'")) ///
		(rcap aux_high aux_high_tip aux_left, color(black) msize(0)) ///
		(rcap aux_high aux_high_tip aux_right, color(black) msize(0)) ///
		(rcap aux_leftleft aux_leftright aux_low, horizontal color(black) msize(0) text(`max' 0 "`Psocial_proximity'")) ///
		(rcap aux_low aux_low_tip aux_leftleft, color(black) msize(0)) ///
		(rcap aux_low aux_low_tip aux_leftright, color(black) msize(0)) ///
		(rcap aux_rightleft aux_rightright aux_low, horizontal color(black) msize(0) text(`max' 1 "`Hsocial_proximity'")) ///
		(rcap aux_low aux_low_tip aux_rightleft, color(black) msize(0)) ///
		(rcap aux_low aux_low_tip aux_rightright, color(black) msize(0)) ///
		(rcap aux_leftleft aux_rightleft aux_pill_low, horizontal color(black) msize(0) text(`pilllow' `=0.5-`offset'' "`diffsocial_proximity'")) ///
		(rcap aux_pill_low aux_pill_low_tip aux_leftleft, color(black) msize(0)) ///
		(rcap aux_pill_low aux_pill_low_tip aux_rightleft, color(black) msize(0)) ///
 		(rcap aux_leftright aux_rightright aux_pill_high, horizontal color(black) msize(0) text(`pillhigh' `=0.5+`offset'' "`samesocial_proximity'")) ///
		(rcap aux_pill_high aux_pill_high_tip aux_leftright, color(black) msize(0)) ///
		(rcap aux_pill_high aux_pill_high_tip aux_rightright, color(black) msize(0)) ///
 		, name(social_proximity, replace) scale(0.8) ///
		$bar_design ytitle("Perceived normalized social proximity", size(large)) xtitle("") ///
		subtitle("`subtitlesocial_proximity'", bcolor(white) size(large)) xscale(r(-0.5 1.5)) ///
		xlabel(0 "Placebo" 1 "Hydrocortisone", labsize(4.5) noticks) ylabel(0(0.1)`=`maxmax'+0.025', nogrid) legend(off)
graph combine social_proximity, fxsize(40) nodraw name(social_proximity, replace) graphregion(margin(0 0 0 0) color(white)) imargin(0 1 0 0)

********************************************************************************
************************* Social Proximity Components **************************
********************************************************************************

sum decision_h if game_str == "likely_to_be_friends" | game_str == "trust" | game_str == "closeness"
local maxmax 				= r(max) 		+ 0.275 	// stars across pills
local max					= r(max) 		+ 0.2 		// stars within pill
local pillhigh 				= r(max) 		+ 0.125		// stars within hydro pill
local pilllow 				= r(max) 		+ 0.05		// stars within placebo pill
replace aux_low				= `max' 		- 0.03 		// bracket within pills (bar)
replace aux_low_tip			= `max' 		- 0.035		// bracket within pills (tips)
replace aux_high			= `maxmax' 		- 0.03 		// bracket across pills (bar)
replace aux_high_tip		= `maxmax' 		- 0.035		// bracket across pills (tips)
replace aux_pill_high		= `pillhigh' 	- 0.03		// bracket within hydro pill (bar)
replace aux_pill_high_tip	= `pillhigh' 	- 0.035		// bracket within hydro pill (tips)
replace aux_pill_low		= `pilllow' 	- 0.03		// bracket within placebo pill (bar)
replace aux_pill_low_tip	= `pilllow' 	- 0.035		// bracket within placebo pill (tips)

local yscale    					yscale(r(2(0.5)4))
local yscalelikely_to_be_friends	yscale(r(2(0.5)4))
local yscaletrust 					yscale(r(2(0.5)4) off)
local yscalecloseness 				yscale(r(2(0.5)4) off)
local fxsizelikely_to_be_friends 	48
local fxsizetrust
local fxsizecloseness
local legendlikely_to_be_friends	legend(off)
local legendtrust					legend(off)
local legendcloseness 				legend(size(medium) order(1 "Different Ethnicity" 2 "Same Ethnicity" 5 "{&plusminus} 1 SE" 88 " " 88 " " 88 " " 88 " " 88 " " 88 " " 88 " " 88 " ") cols(1) region(color(none)) bplacement(c) ring(0))

foreach game in likely_to_be_friends trust closeness {
foreach scale_insert in "" "`game'" { // Run the middle parts of the graph twice, once giving tg1 and tg2 a y-axis and saving them and once leaving it out and preparing it for graph combine
twoway  (bar decision offset if game_str == "`game'" & !treatment & !_same_eth, barwidth(`barwidth') fcolor(none) lcolor($barcolor) lwidth(medium)) ///
		(bar decision offset if game_str == "`game'" & !treatment &  _same_eth, barwidth(`barwidth')  color($barcolor)) ///
		(bar decision offset if game_str == "`game'" &  treatment & !_same_eth, barwidth(`barwidth') fcolor(none) lcolor($barcolor) lwidth(medium)) ///
		(bar decision offset if game_str == "`game'" &  treatment &  _same_eth, barwidth(`barwidth')  color($barcolor)) ///
		(rcap decision_h decision_l offset if game_str == "`game'" & !treatment & !_same_eth, color($rcapcolor)) ///
		(rcap decision_h decision_l offset if game_str == "`game'" & !treatment &  _same_eth, color($rcapcolor)) ///
		(rcap decision_h decision_l offset if game_str == "`game'" &  treatment & !_same_eth, color($rcapcolor)) ///
		(rcap decision_h decision_l offset if game_str == "`game'" &  treatment &  _same_eth, color($rcapcolor)) ///
		(rcap aux_left aux_right aux_high, horizontal color(black) msize(0) text(`maxmax' 0.5 "`D`game''")) ///
		(rcap aux_high aux_high_tip aux_left, color(black) msize(0)) ///
		(rcap aux_high aux_high_tip aux_right, color(black) msize(0)) ///
		(rcap aux_leftleft aux_leftright aux_low, horizontal color(black) msize(0) text(`max' 0 "`P`game''")) ///
		(rcap aux_low aux_low_tip aux_leftleft, color(black) msize(0)) ///
		(rcap aux_low aux_low_tip aux_leftright, color(black) msize(0)) ///
		(rcap aux_rightleft aux_rightright aux_low, horizontal color(black) msize(0) text(`max' 1 "`H`game''")) /// 
		(rcap aux_low aux_low_tip aux_rightleft, color(black) msize(0)) ///
		(rcap aux_low aux_low_tip aux_rightright, color(black) msize(0)) ///
		(rcap aux_leftleft aux_rightleft aux_pill_low, horizontal color(black) msize(0) text(`pilllow' `=0.5-`offset'' "`diff`game''")) ///
		(rcap aux_pill_low aux_pill_low_tip aux_leftleft, color(black) msize(0)) ///
		(rcap aux_pill_low aux_pill_low_tip aux_rightleft, color(black) msize(0)) ///
 		(rcap aux_leftright aux_rightright aux_pill_high, horizontal color(black) msize(0) text(`pillhigh' `=0.5+`offset'' "`same`game''")) ///
		(rcap aux_pill_high aux_pill_high_tip aux_leftright, color(black) msize(0)) ///
		(rcap aux_pill_high aux_pill_high_tip aux_rightright, color(black) msize(0)) ///
 		, name(`game', replace) scale(1.0) ///
		$bar_design ytitle("Average answer", size(large)) xtitle("") `yscale`scale_insert'' ///
		subtitle("`subtitle`game''", bcolor(white) size(large)) xscale(r(-0.5 1.5)) ///
		xlabel(0 "Placebo" 1 "Hydrocortisone", labsize(medlarge) noticks) ylabel(2(0.5)4, nogrid) `legend`game''
graph combine `game', fxsize(`fxsize`game'') nodraw name(`game', replace) graphregion(margin(0 0 0 0) color(white)) imargin(0 1 0 0)
}
}
graph combine likely_to_be_friends trust closeness,  name(social_proximity_components, replace) ycommon imargin(0 1 0 0) rows(1) graphregion(color(white))
graph export "$figures_dir/mean_comparison_social_proximity_components.pdf", replace

********************************************************************************
******************************* Composite Panel ********************************
********************************************************************************

graph combine cyd allocations social_proximity, xsize(7.5) graphregion(margin(0 0 0 0) color(white)) imargin(0 1 0 0) rows(1)
graph export "$figures_dir/mean_comparison_full_with_pills.pdf", replace

noisily dis as input "End: Mean comparisons"
